<?php

/** 
 * Copyright (c) 2012, Jiehun.com.cn Inc. All Rights Reserved
 * @author huqingping@jiehun.com.cn
 * @date 2012-2-28
 * @version 1.0 
 * @brief 
 *  
 **/
require_once dirname(__FILE__).'/sphinxapi.php';
 
final class Sphinx
{
	var $api;
	var $conf;
	function __construct($conf)
	{
		$this->conf = $conf;
		$this->init();
	}
	
	private function init()
	{
		if (!$this->api) {
			$this->api = new SphinxClient();
        	$this->api->SetServer($this->conf['ip'],$this->conf['port']);
        	$this->api->SetConnectTimeout($this->conf['connect_timeout']);
       	 	$this->api->SetArrayResult (true);
		}
	}
	
	function __call($name,$args)
	{
		$this->init();
		$ret = call_user_func_array(array($this->api,ucfirst($name)),$args);
		if ($name == 'runQueries' ||
			$name == 'query') {
				//被Com 模块缓存了，不能反复使用，
				//最后执行搜索后，这里清空一下
				$this->api = null;
			}
		return $ret;
	}
}
